<template>
  <div>
    <h2>购物车</h2>
    <table border="1" cellpadding="0" cellspacing="0">
      <thead>
        <tr>
          <td>商品</td>
          <td>单价</td>
          <td>价格</td>
          <td>总量</td>
          <td>操作</td>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(cart, index) in data" :key="index">
          <td>{{ cart.text }}</td>
          <td>{{ cart.price }}</td>
          <td>{{ cart.price * cart.count }}</td>
          <td>{{ cart.count }}</td>
          <td>
            <button @click="reduce(index)">-</button>&nbsp;&nbsp;
            <button @click="add(index)">+</button>
          </td>
        </tr>
        <tr>
            <td colspan="5" v-if="total>0">总价:{{total}}元</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  props: ["data"],
  name: "",
  data() {
    return {};
  },
  computed: {
      total(){
          return this.data.reduce((sum,v)=>{
              return sum+v.price*v.count
          },0)
      }
  },
  methods: {
    add(index) {
      this.$emit("add", index);
    },
    reduce(index) {
      this.$emit("reduce", index);
    },
  },
};
</script>

<style scoped></style>
